package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.IUserInfoDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.UserInfoVo;

public class UserInfoDaoImpl extends JdbcDaoSupport implements IUserInfoDao {
	private static Logger logger = Logger.getLogger(UserInfoDaoImpl.class);
	
	/**根据用户名和密码获得用户登录信息*/
	@SuppressWarnings("unchecked")
	@Override
	public UserInfoVo getUserInfo(String login_username,String login_password) {
		String sql = "select id, login_username, user_key, email, mobile, category from tb_ivr_user_info where login_username = ? and login_password = ? and (category = 0 or category = 1) and status = 1";
		Object[] param = new Object[]{login_username, login_password};
		logger.info(String.format("根据用户名和密码获得用户登录信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<UserInfoVo> userInfoVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(userInfoVoList != null && userInfoVoList.size() > 0){
			return userInfoVoList.get(0);
		}
		return null;
	}
	/**根据密匙获得用户登录信息*/
	@SuppressWarnings("unchecked")
	@Override
	public UserInfoVo getUserInfoByUserKey(String user_key) {
		String sql = "select id, login_username, user_key, email, mobile, category from tb_ivr_user_info where user_key = ? and category = 5 and status = 1";
		Object[] param = new Object[]{user_key};
		logger.info(String.format("根据密匙获得用户登录信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<UserInfoVo> userInfoVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(userInfoVoList != null && userInfoVoList.size() > 0){
			return userInfoVoList.get(0);
		}
		return null;
	}
	/**根据密匙获得资方登录信息*/
	@SuppressWarnings("unchecked")
	@Override
	public UserInfoVo getUserInfoByCaptialKey(String user_key) {
		String sql = "select id, login_username, user_key, email, mobile, category from tb_ivr_user_info where user_key = ? and category = 10 and status = 1";
		Object[] param = new Object[]{user_key};
		logger.info(String.format("根据密匙获得资方登录信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<UserInfoVo> userInfoVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(userInfoVoList != null && userInfoVoList.size() > 0){
			return userInfoVoList.get(0);
		}
		return null;
	}
	/**查询用户信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<UserInfoVo> getUserInfoList() {
		String sql = "select id, login_username, user_key, email, mobile, category from tb_ivr_user_info where status = 1";
		logger.info(String.format("查询用户信息列表[%s]", sql));
		
		List<UserInfoVo> userInfoVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(userInfoVoList != null && userInfoVoList.size() > 0){
			return userInfoVoList;
		}
		return null;
	}
	/**获得合作方账户信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<UserInfoVo> getPartnerUserInfoList() {
		String sql = "select id, login_username, user_key, email, mobile, category from tb_ivr_user_info where category = 5 and status = 1";
		logger.info(String.format("查询用户信息列表[%s]", sql));
		
		List<UserInfoVo> userInfoVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(userInfoVoList != null && userInfoVoList.size() > 0){
			return userInfoVoList;
		}
		return null;
	}
	/**根据用户Id查询用户信息*/
	@SuppressWarnings("unchecked")
	@Override
	public UserInfoVo getUserInfoById(int id) {
		String sql = "select id, login_username, login_password, email, mobile, category from tb_ivr_user_info where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("根据用户Id查询用户信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<UserInfoVo> userInfoVoList = this.getJdbcTemplate().query(sql, param, platUserMapper);
		if(userInfoVoList != null && userInfoVoList.size() > 0){
			return userInfoVoList.get(0);
		}
		return null;
	}
	private ParameterizedRowMapper<UserInfoVo> rowMapper = new ParameterizedRowMapper<UserInfoVo>(){
		@Override
		public UserInfoVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserInfoVo userInfoVo = new UserInfoVo();
			userInfoVo.setId(rs.getInt("id"));
			userInfoVo.setLogin_username(rs.getString("login_username"));
			userInfoVo.setUser_key(rs.getString("user_key"));
			userInfoVo.setEmail(rs.getString("email"));
			userInfoVo.setMobile(rs.getString("mobile"));
			userInfoVo.setCategory(rs.getInt("category"));
			
			return userInfoVo;
		}
	};
	private ParameterizedRowMapper<UserInfoVo> platUserMapper = new ParameterizedRowMapper<UserInfoVo>(){
		@Override
		public UserInfoVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserInfoVo userInfoVo = new UserInfoVo();
			userInfoVo.setId(rs.getInt("id"));
			userInfoVo.setLogin_username(rs.getString("login_username"));
			userInfoVo.setLogin_password(rs.getString("login_password"));
			userInfoVo.setEmail(rs.getString("email"));
			userInfoVo.setMobile(rs.getString("mobile"));
			userInfoVo.setCategory(rs.getInt("category"));
			
			return userInfoVo;
		}
	};
	/**保存用户信息*/
	@Override
	public int insertUserInfo(UserInfoVo userInfoVo) {
		String sql = "insert into tb_ivr_user_info(login_username,login_password,user_key,email,mobile,category,status) values(?,?,?,?,?,?,1)";
		Object[] param = new Object[]{userInfoVo.getLogin_username(),userInfoVo.getLogin_password(),
									userInfoVo.getUser_key(), userInfoVo.getEmail(),
									userInfoVo.getMobile(), userInfoVo.getCategory()};
		logger.info(String.format("保存用户信息[%s]", BaseUtil.logSQL(sql, param)));
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**更新用户信息*/
	@Override
	public int updateUserInfo(UserInfoVo userInfoVo) {
		String sql = "update tb_ivr_user_info set login_password = ?, user_key = ?, email = ?, mobile = ?, category = ? where id = ?";
		Object[] param = new Object[]{userInfoVo.getLogin_password(), userInfoVo.getUser_key(), 
									userInfoVo.getEmail(),userInfoVo.getMobile(), 
									userInfoVo.getCategory(), userInfoVo.getId()};
		logger.info(String.format("更新用户信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	
	/**删除用户信息*/
	@Override
	public int deleteUserInfo(int id) {
		String sql = "update tb_ivr_user_info set status = 0 where id = ?";
		Object[] param = new Object[]{id};
		logger.info(String.format("删除用户信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
}